using System.Collections.Generic;
using Common;
using TMPro;
using UnityEngine;
using UnityEngine.EventSystems;

namespace CardGameObject.GameScene
{
    public class GameTurnButton : MonoBehaviour
    {
        private const string PLAYER_TURN = "回合结束";
        private const string AI_TURN = "对手回合";

        public TextMeshPro TurnText;

        public PlayerRoleEnum CurTurnRole { get; set; } = PlayerRoleEnum.PLAYER;
        public int TurnCount { get; set; } = 0;
        public GameManager GameManager { get; set; }

        public GameMana PlayerGameMana { get; set; }

        public GameMana AiGameMana { get; set; }

        private void Start()
        {
            GameManager = GameManager.GetInstance();
            PlayerGameMana = GameManager.PlayerMana.GetComponent<GameMana>();
            AiGameMana = GameManager.AiMana.GetComponent<GameMana>();

            GameManager.PlayerDeck.GetComponent<GameDeck>().DrawCards(5);
            GameManager.AiDeck.GetComponent<GameDeck>().DrawCards(5);
            PlayerGameMana.IncreaseManaWhenTurnStart();
            UpdateTurnText();
        }

        private void OnMouseDown()
        {
            if (EventSystem.current.IsPointerOverGameObject())
            {
                return;
            }
            if (CurTurnRole == PlayerRoleEnum.PLAYER)
            {
                CurTurnRole = PlayerRoleEnum.AI;
                AiGameMana.IncreaseManaWhenTurnStart();
                GameManager.AiDeck.GetComponent<GameDeck>().DrawCards(1);
                List<GameObject> monsterList = GameManager.AiMonster;
                foreach (GameObject monster in monsterList)
                {
                    monster.GetComponent<GameCard>().AttackCountPerTurn = 1;
                }
            }
            else
            {
                CurTurnRole = PlayerRoleEnum.PLAYER;
                PlayerGameMana.IncreaseManaWhenTurnStart();
                GameManager.PlayerDeck.GetComponent<GameDeck>().DrawCards(1);
                List<GameObject> monsterList = GameManager.PlayerMonster;
                foreach (GameObject monster in monsterList)
                {
                    monster.GetComponent<GameCard>().AttackCountPerTurn = 1;
                }
            }
            TurnCount++;
            UpdateTurnText();
        }

        private void UpdateTurnText()
        {
            if(CurTurnRole == PlayerRoleEnum.PLAYER)
            {
                TurnText.text = PLAYER_TURN;
            }
            else
            {
                TurnText.text = AI_TURN;
            }
        }
    }
}